package cn.skycity.common.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.skycity.common.service.ISysTenantService;
import cn.skycity.common.service.SysConfigService;
import cn.skycity.framework.core.model.Result;
import com.skycity.framework.uaa.pojo.entity.SysTenant;
import com.skycity.framework.uaa.pojo.vo.LoginTenantVo;
import com.skycity.framework.uaa.pojo.vo.TenantListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 租户表 前端控制器
 * </p>
 *
 * @author YingBo.Dai
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/tenant")
public class SysTenantController {
    @Autowired
    private SysConfigService configService;

    @Autowired
    private ISysTenantService sysTenantService;

    @GetMapping("/list")
    public Result<LoginTenantVo> tenantList() {
        LoginTenantVo result = new LoginTenantVo();
        String tenantEnabled = configService.getConfigKey("tenant.enable");
        // 租户是否开启
        boolean enable = "Y".equals(tenantEnabled);
        result.setTenantEnabled(enable);
        // 如果未开启租户这直接返回
        if (!enable) {
            return Result.success(result);
        }
        List<SysTenant> tenantList = sysTenantService.list();
        List<TenantListVo> tenantListVos = BeanUtil.copyToList(tenantList, TenantListVo.class);
        result.setVoList(tenantListVos);
        return Result.success(result);
    }
}
